function restrictInput(field,type,needed) {

	$(field).attr("regType",type);
	
	if(needed == "true") {
		$(field).attr("needed",needed);
	}
	
	if(type != "mail") {
		/*$(field).keydown(function(evt){
			var currReg = getReg($(this).attr("regType"));
			var key = String.fromCharCode(evt.keyCode);
			if(!currReg.test(key) && !isFunctionalKey(evt.keyCode)) {
				return false;
			}
		});
		*/
		$(field).keyup(function(evt){
			var isValid = false;
			var currReg = getReg($(this).attr("regType"));
			
			while(!isValid) {
				var value = $(this).val();
				isValid = currReg.test(value);
				
				if(!isValid) {
					value = value.substring(0, value.length-1);
					$(this).val(value);
				}
			}
		});
	}
}

// ----------------------------------------------
function getReg(type) {
	var regExp;
	
	switch(type) {
		case "number":
			regExp = numReg;
		break;
		
		case "text":
			regExp = txtReg;
		break;
		
		case "textAndNum":
			regExp = textAndNumReg;
		break;
		
		case "hybrid":
			regExp = hybridReg;
		break;
		
		case "phone":
			regExp = phoneReg;
		break;
		
		case "mail":
			regExp = emailReg;
		break;
	}
	return regExp;
}

// ----------------------------------------------
var functionalKeys = [8,36,45,46,37,38,39,40,13,16,17,18,9,96,110,97,98,99,100,101,103,104,105];
function isFunctionalKey(keyCode) {
	var isFunctionalKey = false;
	
	for(var i = 0; i < functionalKeys.length; i++) {
		if(keyCode == functionalKeys[i]) {
			isFunctionalKey = true;
		}
	}
	return isFunctionalKey;
}

// =====================================================
// ================ REG EXPs ===========================
// =====================================================

var txtReg 		= /(^[a-zA-Z\u00C0\u00C1\u00C2\u00C7\u00C8\u00C9\u00CA\u00CC\u00CD\u00CE\u00D1\u00D2\u00D3\u00D4\u00D9\u00DA\u00DB\u00E0\u00E1\u00E2\u00E7\u00E8\u00E9\u00EA\u00EC\u00ED\u00EE\u00F1\u00F2\u00F3\u00F4\u00F9\u00FA\u00FB ]*$)/i;
var numReg 		= /(^([0-9])*$)/i;
var phoneReg 	= /(^([0-9\-\(\)\+ ])*$)/i;
var emailReg	= /(^[a-z0-9]([a-z0-9_\.\-]*)@([a-z0-9_\.\-]*)([.][a-z]{2,6})$)|(^[a-z0-9]([a-z_\.\-]*)@([a-z0-9_\.\-]*)(\.[a-z]{2,6})(\.[a-z]{6})*$)/i;
var passReg		= /(^([a-zA-Z0-9])*$)/i;
var textAndNumReg = /(^[0-9a-zA-Z\u00C0\u00C1\u00C2\u00C7\u00C8\u00C9\u00CA\u00CC\u00CD\u00CE\u00D1\u00D2\u00D3\u00D4\u00D9\u00DA\u00DB\u00E0\u00E1\u00E2\u00E7\u00E8\u00E9\u00EA\u00EC\u00ED\u00EE\u00F1\u00F2\u00F3\u00F4\u00F9\u00FA\u00FB]*$)/i;
var hybridReg = /(^[0-9a-zA-Z\u00C0\u00C1\u00C2\u00C7\u00C8\u00C9\u00CA\u00CC\u00CD\u00CE\u00D1\u00D2\u00D3\u00D4\u00D9\u00DA\u00DB\u00E0\u00E1\u00E2\u00E7\u00E8\u00E9\u00EA\u00EC\u00ED\u00EE\u00F1\u00F2\u00F3\u00F4\u00F9\u00FA\u00FB ]*$)/i;


// =====================================================
// ================ FORM VALIDATOR =====================
// =====================================================

function validateForm(obj)
{
	var isValidForm = true;
	$(obj).find("input").each(function(){
		if($(this).attr("regType") && !validateField($(this))) {
			isValidForm = false;
		}
	});
	
	$(obj).find("textarea").each(function(){
		if($(this).attr("regType") && !validateField($(this))) {
			isValidForm = false;
		}
	});
	
	$(obj).find("select").each(function(){
		if($(this).attr("regType") && !validateField($(this))) {
			isValidForm = false;
		}
	});

	return isValidForm;
}

function validateField(field) 
{
	var isValid = true;
	var needsValidation = false;

	if($(field).attr("regType") != undefined)
	{
		needsValidation = true;
		var currReg = getReg($(field).attr("regType"));
		if(currReg) {
			isValid = currReg.test($(field).val());
		}
		
	}
	
	if($(field).attr("needed") == "true") {
		var value = $(field).val();
		if(value.length < 3) {
			isValid = false;
		}
	}
	var validationSign = $("#"+$(field).attr("id")+"Validation");
	if(validationSign) {

		var rightClassName = $("#validationRightClass").val();
		var wrongClassName = $("#validationWrongClass").val();
		var neutralClassName = $("#validationNeutralClass").val();
		
		if(needsValidation && isValid) {
			// IS RIGHT
			validationSign.addClass(rightClassName);
			validationSign.removeClass(wrongClassName);
			validationSign.removeClass(neutralClassName);
			
		}else if(needsValidation && !isValid) {
			//IS WRONG
			validationSign.removeClass(rightClassName);
			validationSign.addClass(wrongClassName);
			validationSign.removeClass(neutralClassName);
		} else {
			//IS NEUTRAL
			validationSign.removeClass(rightClassName);
			validationSign.removeClass(wrongClassName);
			validationSign.addClass(neutralClassName);
		}
		
	}
	return isValid;
}
